package com.chen.androidstudy.activity.recycle;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chen.androidstudy.R;
import com.chen.androidstudy.utils.ScreenUtils;

/**
 * @Author: CHong
 * @CreateDate: 2024/9/3 0003
 * @Description:
 */

public class RecycleViewScrollActivity extends AppCompatActivity {
    RecyclerView.OnScrollListener mScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (newState == RecyclerView.SCROLL_STATE_IDLE){
                getScrollPosition();
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    };

    private void getScrollPosition(){
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if(manager instanceof LinearLayoutManager){
            LinearLayoutManager layoutManager = (LinearLayoutManager) manager;
            int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
            int lastVisibleItemPosition = layoutManager.findLastVisibleItemPosition();
            int firstCompletelyVisibleItemPosition = layoutManager.findFirstCompletelyVisibleItemPosition();
            int lastCompletelyVisibleItemPosition = layoutManager.findLastCompletelyVisibleItemPosition();
            Log.e("chen","firstVisibleItemPosition:" + firstVisibleItemPosition);
            Log.e("chen","lastVisibleItemPosition:" + lastVisibleItemPosition);
            Log.e("chen","firstCompletelyVisibleItemPosition:" + firstCompletelyVisibleItemPosition);
            Log.e("chen","lastCompletelyVisibleItemPosition:" + lastCompletelyVisibleItemPosition);
            // 判断获取的position不为-1，则为有效数据
            if (firstCompletelyVisibleItemPosition != -1) {
                //对应的ItemView
                ConstraintLayout view = (ConstraintLayout) layoutManager.findViewByPosition(firstCompletelyVisibleItemPosition);
                // 获取当前选项卡的viewHolder实例，可以自行做业务逻辑处理
                RecyclerView.ViewHolder viewHolder = recyclerView.findViewHolderForAdapterPosition(firstCompletelyVisibleItemPosition);
                if (viewHolder != null) {
                }
            }
        }
    }

    private void setViewAplha(View view){
        if (view == null )
            return;
        float beginPercent = 0.1f;
        float endValue = 1;
        float p = ScreenUtils.px2dp(this, Math.abs((int) view.getY())) * 1.0f / ScreenUtils.px2dp(this, view.getHeight()) * 1.0f;
        float curPercent = Float.compare(p - beginPercent, 0.0f) < 0 ? 0.0f : p - beginPercent;
        curPercent = Float.compare(1, curPercent * endValue) < 0 ? 1 : curPercent * endValue;
        view.setAlpha(1 - curPercent);
    }
    RecyclerView recyclerView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycle_view_layout);
        recyclerView = findViewById(R.id.recyclerView);

        recyclerView.addOnScrollListener(mScrollListener);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(new RecycleViewItemAdapter());
    }
}
